package com.aipanda.entity;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

/**
 * 区域类
 * 
 * @param name  	区域名称
 * @param fullName  全名
 * @param treePath  区域路径
 * @param parent    区域父类
 * @param children  子节点
 * @param members   会员
 * @param receivers  收件人
 * @param orders     订单
 * @param deliveryCenters 发货中心
 * @author wanyuxiang
 * 
 */
@SuppressWarnings("unchecked")
@Entity
@Table(name = "aipanda_area")
public class Area extends OrderEntity {
	private static final long serialVersionUID = -2158109459123036967L;
	private String name;
	private String fullName;
	private String treePath;
	private Area parent;
	private Set<Area> children = new HashSet();
	private Set<Member> members = new HashSet();
	private Set<Receiver> receivers = new HashSet();
	private Set<Order> orders = new HashSet();
	private Set<DeliveryCenter> deliveryCenters = new HashSet();

	@NotEmpty
	@Length(max = 100)
	@Column(nullable = false, length = 100)
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name="full_name",nullable = false, length = 500)
	public String getFullName() {
		return this.fullName;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}
	@Column(name="tree_path",nullable = false, updatable = false)
	public String getTreePath() {
		return this.treePath;
	}

	public void setTreePath(String treePath) {
		this.treePath = treePath;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="parent")
	public Area getParent() {
		return this.parent;
	}

	public void setParent(Area parent) {
		this.parent = parent;
	}

	@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = {CascadeType.REMOVE})
	@OrderBy("order asc")
	public Set<Area> getChildren() {
		return this.children;
	}

	public void setChildren(Set<Area> children) {
		this.children = children;
	}

	@OneToMany(mappedBy = "area", fetch = FetchType.LAZY)
	public Set<Member> getMembers() {
		return this.members;
	}

	public void setMembers(Set<Member> members) {
		this.members = members;
	}

	@OneToMany(mappedBy = "area", fetch = FetchType.LAZY)
	public Set<Receiver> getReceivers() {
		return this.receivers;
	}

	public void setReceivers(Set<Receiver> receivers) {
		this.receivers = receivers;
	}

	@OneToMany(mappedBy = "area", fetch = FetchType.LAZY)
	public Set<Order> getOrders() {
		return this.orders;
	}

	public void setOrders(Set<Order> orders) {
		this.orders = orders;
	}

	@OneToMany(mappedBy = "area", fetch = FetchType.LAZY)
	public Set<DeliveryCenter> getDeliveryCenters() {
		return this.deliveryCenters;
	}

	public void setDeliveryCenters(Set<DeliveryCenter> deliveryCenters) {
		this.deliveryCenters = deliveryCenters;
	}

	public Area prePersist(Area area) {
		Area currentArea = area.getParent();
		if (currentArea != null) {
			area.setFullName(currentArea.getFullName() + area.getName());
			area.setTreePath(currentArea.getTreePath() + currentArea.getId() + ",");
		} else {
			area.setFullName(getName());
			area.setTreePath(",");
		}
		return area;
	}

	public Area preUpdate(Area area) {
		Area parentnArea = area.getParent();
		if (parentnArea != null){
			area.setFullName(parentnArea.getFullName() + area.getName());
		}else{
			area.setFullName(area.getName());
		}
		return area;
	}

	public Area preRemove(Area area) {
		Set<Member> setMember = area.getMembers();
		Set<Receiver> setReceiver = null;
		Set<Order> setOrder = null;
		if (setMember != null) {
			Iterator<Member> iterator = setMember.iterator();
			while (iterator.hasNext()) {
				Member member = iterator.next();
				member.setArea(null);
			}
		} else {
			setReceiver = area.getReceivers();
		}
		if (setReceiver != null) {
			Iterator<Receiver> iteratorReceiver = setReceiver.iterator();
			while (iteratorReceiver.hasNext()) {
				Receiver receiver = iteratorReceiver.next();
				receiver.setArea(null);
			}
		} else {
			setOrder = area.getOrders();
		}
		if (setOrder != null) {
			Iterator<Order> iteratorOrders = setOrder.iterator();
			while (iteratorOrders.hasNext()) {
				Order order = iteratorOrders.next();
				order.setArea(null);
			}
		}
		Set<DeliveryCenter> setDeliveryCenter = area.getDeliveryCenters();
		if (setDeliveryCenter != null) {
			Iterator<DeliveryCenter> iteratorDeliveryCenter = setDeliveryCenter.iterator();
			while (iteratorDeliveryCenter.hasNext()) {
				DeliveryCenter deliveryCenter = iteratorDeliveryCenter.next();
				deliveryCenter.setArea(null);
			}
		}
		return area;
	}

	public String toString() {
		return getFullName();
	}
}
